home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / dix / property.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-14  |  15.1 KB  |  540 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: property.c,v 5.2 89/09/11 18:25:40 rws Exp $ */
  25.  
  26. #include "X.h"
  27. #define NEED_REPLIES
  28. #define NEED_EVENTS
  29. #include "Xproto.h"
  30. #include "windowstr.h"
  31. #include "propertyst.h"
  32. #include "dixstruct.h"
  33.  
  34. extern void (*ReplySwapVector[]) ();
  35. extern void CopySwap16Write(), CopySwap32Write(), Swap32Write();
  36. extern int WriteToClient();
  37.  
  38. /*****************************************************************
  39.  * Property Stuff
  40.  *
  41.  *    ChangeProperty, DeleteProperty, GetProperties,
  42.  *    ListProperties
  43.  *
  44.  *   Properties below to windows.  A allocate slots each time
  45.  *   a property is added.  No fancy searching done.
  46.  *
  47.  *****************************************************************/
  48.  
  49. #ifdef notdef
  50. static void
  51. PrintPropertys(pWin)
  52.     WindowPtr pWin;
  53. {
  54.     PropertyPtr pProp;
  55.     register int j;
  56.  
  57.     pProp = pWin->userProps;
  58.     while (pProp)
  59.     {
  60.         ErrorF(  "%x %x\n", pProp->propertyName, pProp->type);
  61.         ErrorF("property format: %d\n", pProp->format);
  62.         ErrorF("property data: \n");
  63.         for (j=0; j<(pProp->format/8)*pProp->size; j++)
  64.            ErrorF("%c\n", pProp->data[j]);
  65.         pProp = pProp->next;
  66.     }
  67. }
  68. #endif
  69.  
  70. int
  71. ProcRotateProperties(client)
  72.     ClientPtr client;
  73. {
  74.     int     i, j, delta;
  75.     REQUEST(xRotatePropertiesReq);
  76.     WindowPtr pWin;
  77.     register    Atom * atoms;
  78.     PropertyPtr * props;               /* array of pointer */
  79.     PropertyPtr pProp;
  80.     xEvent event;
  81.  
  82.     REQUEST_FIXED_SIZE(xRotatePropertiesReq, stuff->nAtoms << 2);
  83.     UpdateCurrentTime();
  84.     pWin = (WindowPtr) LookupWindow(stuff->window, client);
  85.     if (!pWin)
  86.         return(BadWindow);
  87.     if (!stuff->nAtoms)
  88.     return(Success);
  89.     atoms = (Atom *) & stuff[1];
  90.     props = (PropertyPtr *)ALLOCATE_LOCAL(stuff->nAtoms * sizeof(PropertyPtr));
  91.     if (!props)
  92.     return(BadAlloc);
  93.     for (i = 0; i < stuff->nAtoms; i++)
  94.     {
  95.         if (!ValidAtom(atoms[i]))
  96.         {
  97.             DEALLOCATE_LOCAL(props);
  98.         client->errorValue = atoms[i];
  99.             return BadAtom;
  100.         }
  101.         for (j = i + 1; j < stuff->nAtoms; j++)
  102.             if (atoms[j] == atoms[i])
  103.             {
  104.                 DEALLOCATE_LOCAL(props);
  105.                 return BadMatch;
  106.             }
  107.         pProp = wUserProps (pWin);
  108.         while (pProp)
  109.         {
  110.             if (pProp->propertyName == atoms[i])
  111.                 goto found;
  112.         pProp = pProp->next;
  113.         }
  114.         DEALLOCATE_LOCAL(props);
  115.         return BadMatch;
  116. found: 
  117.         props[i] = pProp;
  118.     }
  119.     delta = stuff->nPositions;
  120.  
  121.     /* If the rotation is a complete 360 degrees, then moving the properties
  122.     around and generating PropertyNotify events should be skipped. */
  123.  
  124.     if ( (stuff->nAtoms != 0) && (abs(delta) % stuff->nAtoms) != 0 ) 
  125.     {
  126.     while (delta < 0)                  /* faster if abs value is small */
  127.             delta += stuff->nAtoms;
  128.         for (i = 0; i < stuff->nAtoms; i++)
  129.      {
  130.         /* Generate a PropertyNotify event for each property whose value
  131.         is changed in the order in which they appear in the request. */
  132.  
  133.          event.u.u.type = PropertyNotify;
  134.             event.u.property.window = pWin->drawable.id;
  135.             event.u.property.state = PropertyNewValue;
  136.         event.u.property.atom = props[i]->propertyName;    
  137.         event.u.property.time = currentTime.milliseconds;
  138.         DeliverEvents(pWin, &event, 1, (WindowPtr)NULL);
  139.     
  140.             props[i]->propertyName = atoms[(i + delta) % stuff->nAtoms];
  141.     }
  142.     }
  143.     DEALLOCATE_LOCAL(props);
  144.     return Success;
  145. }
  146.  
  147. int 
  148. ProcChangeProperty(client)
  149.     ClientPtr client;
  150. {          
  151.     WindowPtr pWin;
  152.     char format, mode;
  153.     unsigned long len;
  154.     PropertyPtr pProp;
  155.     xEvent event;
  156.     int sizeInBytes;
  157.     pointer data;
  158.     REQUEST(xChangePropertyReq);
  159.  
  160.     REQUEST_AT_LEAST_SIZE(xChangePropertyReq);
  161.     UpdateCurrentTime();
  162.     format = stuff->format;
  163.     mode = stuff->mode;
  164.     if ((mode != PropModeReplace) && (mode != PropModeAppend) &&
  165.     (mode != PropModePrepend))
  166.     {
  167.     client->errorValue = mode;
  168.     return BadValue;
  169.     }
  170.     if ((format != 8) && (format != 16) && (format != 32))
  171.     {
  172.     client->errorValue = format;
  173.         return BadValue;
  174.     }
  175.     len = stuff->nUnits;
  176.     sizeInBytes = format>>3;
  177.     REQUEST_FIXED_SIZE(xChangePropertyReq, len * sizeInBytes);
  178.  
  179.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  180.     if (!pWin)
  181.     return(BadWindow);
  182.     if (!ValidAtom(stuff->property))
  183.     {
  184.     client->errorValue = stuff->property;
  185.     return(BadAtom);
  186.     }
  187.     if (!ValidAtom(stuff->type))
  188.     {
  189.     client->errorValue = stuff->type;
  190.     return(BadAtom);
  191.     }
  192.  
  193.     /* first see if property already exists */
  194.  
  195.     pProp = wUserProps (pWin);
  196.     while (pProp)
  197.     {
  198.     if (pProp->propertyName ==stuff->property) 
  199.         break;
  200.     pProp = pProp->next;
  201.     }
  202.     if (!pProp)   /* just add to list */
  203.     {
  204.     if (!pWin->optional && !MakeWindowOptional (pWin))
  205.         return(BadAlloc);
  206.         pProp = (PropertyPtr)xalloc(sizeof(PropertyRec));
  207.     if (!pProp)
  208.         return(BadAlloc);
  209.         data = (pointer)xalloc(sizeInBytes  * len);
  210.     if (!data && len)
  211.     {
  212.         xfree(pProp);
  213.         return(BadAlloc);
  214.     }
  215.         pProp->propertyName = stuff->property;
  216.         pProp->type = stuff->type;
  217.         pProp->format = format;
  218.         pProp->data = data;
  219.     if (len)
  220.         bcopy((char *)&stuff[1], (char *)data, (int)(len * sizeInBytes));
  221.     pProp->size = len;
  222.         pProp->next = pWin->optional->userProps;
  223.         pWin->optional->userProps = pProp;
  224.     }
  225.     else
  226.     {
  227.     
  228.     /* To append or prepend to a property the request format and type
  229.         must match those of the already defined property.  The
  230.         existing format and type are irrelevant when using the mode
  231.         "PropModeReplace" since they will be written over. */
  232.  
  233.         if ((format != pProp->format) && (mode != PropModeReplace))
  234.         return(BadMatch);
  235.         if ((pProp->type != stuff->type) && (mode != PropModeReplace))
  236.             return(BadMatch);
  237.         if (mode == PropModeReplace) 
  238.         {
  239.         data = (pointer)xrealloc(pProp->data, sizeInBytes * len);
  240.         if (!data && len)
  241.         return(BadAlloc);
  242.             pProp->data = data;
  243.         if (len)
  244.         bcopy((char *)&stuff[1], (char *)data,
  245.               (int)(len * sizeInBytes));
  246.         pProp->size = len;
  247.             pProp->type = stuff->type;
  248.         pProp->format = stuff->format;
  249.     }
  250.     else if (len == 0)
  251.     {
  252.         /* do nothing */
  253.     }
  254.         else if (mode == PropModeAppend)
  255.         {
  256.         data = (pointer)xrealloc(pProp->data,
  257.                      sizeInBytes * (len + pProp->size));
  258.         if (!data)
  259.         return(BadAlloc);
  260.             pProp->data = data;
  261.         bcopy((char *)&stuff[1],
  262.           &((char *)data)[pProp->size * sizeInBytes], 
  263.           (int)(len * sizeInBytes));
  264.             pProp->size += len;
  265.     }
  266.         else if (mode == PropModePrepend)
  267.         {
  268.             data = (pointer)xalloc(sizeInBytes * (len + pProp->size));
  269.         if (!data)
  270.         return(BadAlloc);
  271.         bcopy((char *)pProp->data, &((char *)data)[len * sizeInBytes], 
  272.           (int)(pProp->size * sizeInBytes));
  273.             bcopy((char *)&stuff[1], (char *)data, (int)(len * sizeInBytes));
  274.         xfree(pProp->data);
  275.             pProp->data = data;
  276.             pProp->size += len;
  277.     }
  278.     }
  279.     event.u.u.type = PropertyNotify;
  280.     event.u.property.window = pWin->drawable.id;
  281.     event.u.property.state = PropertyNewValue;
  282.     event.u.property.atom = pProp->propertyName;
  283.     event.u.property.time = currentTime.milliseconds;
  284.     DeliverEvents(pWin, &event, 1, (WindowPtr)NULL);
  285.  
  286.     return(client->noClientException);
  287. }
  288.  
  289. DeleteProperty(pWin, propName)
  290.     WindowPtr pWin;
  291.     Atom propName;
  292. {
  293.     PropertyPtr pProp, prevProp;
  294.     xEvent event;
  295.  
  296.     if (!(pProp = wUserProps (pWin)))
  297.     return(Success);
  298.     prevProp = (PropertyPtr)NULL;
  299.     while (pProp)
  300.     {
  301.     if (pProp->propertyName == propName)
  302.         break;
  303.         prevProp = pProp;
  304.     pProp = pProp->next;
  305.     }
  306.     if (pProp) 
  307.     {            
  308.         if (prevProp == (PropertyPtr)NULL)      /* takes care of head */
  309.         {
  310.             if (!(pWin->optional->userProps = pProp->next))
  311.         CheckWindowOptionalNeed (pWin);
  312.         }
  313.     else
  314.         {
  315.             prevProp->next = pProp->next;
  316.         }
  317.     event.u.u.type = PropertyNotify;
  318.     event.u.property.window = pWin->drawable.id;
  319.     event.u.property.state = PropertyDelete;
  320.         event.u.property.atom = pProp->propertyName;
  321.     event.u.property.time = currentTime.milliseconds;
  322.     DeliverEvents(pWin, &event, 1, (WindowPtr)NULL);
  323.     xfree(pProp->data);
  324.         xfree(pProp);
  325.     }
  326.     return(Success);
  327. }
  328.  
  329. DeleteAllWindowProperties(pWin)
  330.     WindowPtr pWin;
  331. {
  332.     PropertyPtr pProp, pNextProp;
  333.     xEvent event;
  334.  
  335.     pProp = wUserProps (pWin);
  336.     while (pProp)
  337.     {
  338.     event.u.u.type = PropertyNotify;
  339.     event.u.property.window = pWin->drawable.id;
  340.     event.u.property.state = PropertyDelete;
  341.     event.u.property.atom = pProp->propertyName;
  342.     event.u.property.time = currentTime.milliseconds;
  343.     DeliverEvents(pWin, &event, 1, (WindowPtr)NULL);
  344.     pNextProp = pProp->next;
  345.         xfree(pProp->data);
  346.         xfree(pProp);
  347.     pProp = pNextProp;
  348.     }
  349. }
  350.  
  351. /*****************
  352.  * GetProperty
  353.  *    If type Any is specified, returns the property from the specified
  354.  *    window regardless of its type.  If a type is specified, returns the
  355.  *    property only if its type equals the specified type.
  356.  *    If delete is True and a property is returned, the property is also
  357.  *    deleted from the window and a PropertyNotify event is generated on the
  358.  *    window.
  359.  *****************/
  360.  
  361. int
  362. ProcGetProperty(client)
  363.     ClientPtr client;
  364. {
  365.     PropertyPtr pProp, prevProp;
  366.     unsigned long n, len, ind;
  367.     WindowPtr pWin;
  368.     xGetPropertyReply reply;
  369.     REQUEST(xGetPropertyReq);
  370.  
  371.     REQUEST_SIZE_MATCH(xGetPropertyReq);
  372.     if (stuff->delete)
  373.     UpdateCurrentTime();
  374.     pWin = (WindowPtr)LookupWindow(stuff->window, client);
  375.     if (pWin)
  376.     {
  377.     if (!ValidAtom(stuff->property))
  378.     {
  379.         client->errorValue = stuff->property;
  380.         return(BadAtom);
  381.     }
  382.     if ((stuff->delete != xTrue) && (stuff->delete != xFalse))
  383.     {
  384.         client->errorValue = stuff->delete;
  385.         return(BadValue);
  386.     }
  387.     if ((stuff->type == AnyPropertyType) || ValidAtom(stuff->type))
  388.     {
  389.         pProp = wUserProps (pWin);
  390.             prevProp = (PropertyPtr)NULL;
  391.             while (pProp)
  392.             {
  393.             if (pProp->propertyName == stuff->property) 
  394.                 break;
  395.         prevProp = pProp;
  396.         pProp = pProp->next;
  397.             }
  398.         reply.type = X_Reply;
  399.         reply.sequenceNumber = client->sequence;
  400.             if (pProp) 
  401.             {
  402.  
  403.         /* If the request type and actual type don't match. Return the
  404.         property information, but not the data. */
  405.  
  406.                 if ((stuff->type != pProp->type) &&
  407.             (stuff->type != AnyPropertyType))
  408.         {
  409.             reply.bytesAfter = pProp->size;
  410.             reply.format = pProp->format;
  411.             reply.length = 0;
  412.             reply.nItems = 0;
  413.             reply.propertyType = pProp->type;
  414.             WriteReplyToClient(client, sizeof(xGenericReply), &reply);
  415.             return(Success);
  416.         }
  417.  
  418.         /*
  419.              *  Return type, format, value to client
  420.              */
  421.         n = (pProp->format/8) * pProp->size; /* size (bytes) of prop */
  422.         ind = stuff->longOffset << 2;        
  423.  
  424.                /* If longOffset is invalid such that it causes "len" to
  425.                         be negative, it's a value error. */
  426.  
  427.         if (n < ind)
  428.         {
  429.             client->errorValue = stuff->longOffset;
  430.             return BadValue;
  431.         }
  432.  
  433.         len = min(n - ind, 4 * stuff->longLength);
  434.  
  435.         reply.bytesAfter = n - (ind + len);
  436.         reply.format = pProp->format;
  437.         reply.length = (len + 3) >> 2;
  438.         reply.nItems = len / (pProp->format / 8 );
  439.         reply.propertyType = pProp->type;
  440.         WriteReplyToClient(client, sizeof(xGenericReply), &reply);
  441.         if (len)
  442.         {
  443.             switch (reply.format) {
  444.             case 32: client->pSwapReplyFunc = CopySwap32Write; break;
  445.             case 16: client->pSwapReplyFunc = CopySwap16Write; break;
  446.             default: client->pSwapReplyFunc = (void (*) ())WriteToClient; break;
  447.             }
  448.             WriteSwappedDataToClient(client, len, pProp->data + ind);
  449.         }
  450.  
  451.                 if (stuff->delete && (reply.bytesAfter == 0))
  452.                 { /* delete the Property */
  453.             xEvent event;
  454.         
  455.                     if (prevProp == (PropertyPtr)NULL) /* takes care of head */
  456.             {
  457.                         if (!(pWin->optional->userProps = pProp->next))
  458.                 CheckWindowOptionalNeed (pWin);
  459.             }
  460.                 else
  461.                         prevProp->next = pProp->next;
  462.             event.u.u.type = PropertyNotify;
  463.             event.u.property.window = pWin->drawable.id;
  464.             event.u.property.state = PropertyDelete;
  465.             event.u.property.atom = pProp->propertyName;
  466.             event.u.property.time = currentTime.milliseconds;
  467.             DeliverEvents(pWin, &event, 1, (WindowPtr)NULL);
  468.             xfree(pProp->data);
  469.                     xfree(pProp);
  470.         }
  471.         }
  472.             else 
  473.         {   
  474.                 reply.nItems = 0;
  475.         reply.length = 0;
  476.         reply.bytesAfter = 0;
  477.         reply.propertyType = None;
  478.         reply.format = 0;
  479.         WriteReplyToClient(client, sizeof(xGenericReply), &reply);
  480.         }
  481.             return(client->noClientException);
  482.  
  483.     }
  484.         else
  485.     {
  486.         client->errorValue = stuff->type;
  487.             return(BadAtom);
  488.     }
  489.     }
  490.     else            
  491.         return (BadWindow); 
  492. }
  493.  
  494. int
  495. ProcListProperties(client)
  496.     ClientPtr client;
  497. {
  498.     Atom *pAtoms, *temppAtoms;
  499.     xListPropertiesReply xlpr;
  500.     int    numProps = 0;
  501.     WindowPtr pWin;
  502.     PropertyPtr pProp;
  503.     REQUEST(xResourceReq);
  504.  
  505.     REQUEST_SIZE_MATCH(xResourceReq);
  506.     pWin = (WindowPtr)LookupWindow(stuff->id, client);
  507.     if (!pWin)
  508.         return(BadWindow);
  509.  
  510.     pProp = wUserProps (pWin);
  511.     while (pProp)
  512.     {        
  513.         pProp = pProp->next;
  514.     numProps++;
  515.     }
  516.     if (numProps)
  517.         if(!(pAtoms = (Atom *)ALLOCATE_LOCAL(numProps * sizeof(Atom))))
  518.             return(BadAlloc);
  519.  
  520.     xlpr.type = X_Reply;
  521.     xlpr.nProperties = numProps;
  522.     xlpr.length = (numProps * sizeof(Atom)) >> 2;
  523.     xlpr.sequenceNumber = client->sequence;
  524.     pProp = wUserProps (pWin);
  525.     temppAtoms = pAtoms;
  526.     while (pProp)
  527.     {
  528.     *temppAtoms++ = pProp->propertyName;
  529.     pProp = pProp->next;
  530.     }
  531.     WriteReplyToClient(client, sizeof(xGenericReply), &xlpr);
  532.     if (numProps)
  533.     {
  534.         client->pSwapReplyFunc = Swap32Write;
  535.         WriteSwappedDataToClient(client, numProps * sizeof(Atom), pAtoms);
  536.         DEALLOCATE_LOCAL(pAtoms);
  537.     }
  538.     return(client->noClientException);
  539. }
  540.